<?php
namespace app\manager\model;

use think\Model;
use app\manager\common\Crypt;
use \app\common\traits\model\UserOperateCheck;
/**
* 管理员管理
*/
class CampManagers extends \think\Model
{
    use UserOperateCheck;
    protected $table = 'camp_manager';
    protected static $loginmanager = '';
    const FAIL_COUNT = 6;
    const FAIL_EXPIRE = 50;
    const ADMIN_MANAGER = 0;//超级管理员
    const GENERAL_MANAGER = 1;//一般管理员(未锁定)
    const RESTRICTED_MANAGER = -99;//锁定管理员

    public function checkManagerPower($id)
    {
        $status = $this->where('id',$id)->value('status');
        if ($status != self::ADMIN_MANAGER) {
            return false;
        }
        return true;
    }

    public function getStatusAttr($value)
    {
        $status = [0=>'Admin',1=>'管理员',-99=>'锁定管理员'];
        return $status[$value];
    }

////////////////登陆////////////////////

    //判断登陆方式表单(账号密码，手机验证)--->手机发送验证码
    public function loginType($decide)
    {
        //手机验证码登录
        if ($decide = 'mo') {

            $this->mobileLogin();
            # code...
        }
        //账号密码登录
        if ($decede = 'zh') {
            $account=input('post.account');
            $password=input('post.password');
            $this->accountLogin($account,$password,$remember_me = true);
            # code...
        }

    }
    /**
     * 获取账号信息id 手机 邮箱以及判断账号是否存在
     * @param  array $w [description]
     * @return [type]    [description]
     */
    public function getmanager($w)
    {
        return $this->where('phone',$w)->find();
    }
    /**
     * 根据用户id获取用户信息
     * @param  string $user_id [description]
     * @param  string $field   [description]
     * @return [type]          [description]
     */
    protected static function getManagerInfo($manager_id, $field = '')
    {
        if (empty($manager_id)) {
            return ;
        }
        if (empty(self::$loginmanager)) {
            self::$loginmanager = self::where('id',$manager_id)->find();
        }
        if ($field) {
            if (isset(self::$loginmanager[$field])) {
                return self::$loginmanager[$field]; 
            }
            return false;
        }
        return self::$loginmanager;
    }
    /**
     * 获取登录用户信息
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public static function getLoginManagerInfo($field = '')
    {
        return self::getManagerInfo(session('manager.id'), $field);
    }
    public function getLoginUserInfo($field = '')
    {
        return self::getLoginManagerInfo($field);
    }
    /**
     * 使用用户id登陆
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function loginById($manager_id)
    {
        if ($manager_info = $this->getManagerInfo($manager_id)) {
            $this->setManagerSession($manager_info);
            return true;
        }
        return false;
    }
    /**
     * [getPassword 获取账号的密码信息-->判断是否匹配]
     * @param  [type] $user     [description]
     * @param  [type] $password [description]
     * @return [type]           [description]
     */
    public function getPassword($manager,$password)
    {
        $p=$this->getmanager($manager);
        //手机或者邮箱
        if (!($p['password']==md5($password))){
            return false;
        }
        return true;   

    }
    /**
     * [accountLogin 账号密码方式登录]
     * @param  [type]  $account     [description]
     * @param  [type]  $password    [description]
     * @param  boolean $remember_me [description]
     * @return [type]               [description]
     */
    public function accountLogin($account,$password,$remember_me)
    {
        $this->clearManagerSession();
        if ($this->checkOperateCount('managerlogin')) {
            if ($this->getmanager($account)) {
                if ($this->getPassword($account,$password)) {
                    $this->initOperateCount('managerlogin');
                    $data=$this->getmanager($account);//halt($data->getData('status'));
                    if ($data->getData('status') == -99) {
                         $this->error = lang('status is luck');
                         return false;
                    }
                    $this->setManagerSession($data, $remember_me);
                    return true;
                }else{
                    //密码错误
                    $this->error = lang('password is error');
                   //return $this->returnData(lang('password is error'),-1);
                }
            }else{
                //用户不存在
                $this->error = lang('this manager is not empty');
            }
            $this->addOperateCount('managerlogin');
        }else{
            //超限
            $this->error = lang('this manager login fail too many');
        }
        return false;
    }
    /**
     * [checkLogin 判断是否已经登陆]
     * @return [type] [description]
     */
    public function checkLogin()
    {
        //dump(cookie('token'));
        if ($manager_token = cookie('token')) {
             if ($login_manager = $this->getManagerInfo($manager_token)) {
                if (false == session('?manager')) {
                  $this->setManagerSession($login_manager, true);
                      }
                 }    
            }

        if (session('?manager')) {
            $manager_id = $this->getManagerId();
            $login_session_id = $this->getManagerInfo($manager_id, 'login_session_id');
            if ($login_session_id !== session('manager.login_session_id')){
                self::$loginmanager = '';
                $this->logout();
                return false;
            }
        }
        return session('?manager') ? true : false;    
    }
    /**
     * 获取当前登陆用户信息
     */
    public function getLoninManagerInfo()
    {
        $manager=session('manager');
        return $manager;
    }
    public function getManagerId()
    {
        $manager = $this->getLoninManagerInfo();
        return $manager['id'];
    }
    public function getManagerEmail()
    {
        $manager_id = $this->getManagerId();
        $email = $this->where('id',$manager_id)->value('email');
        return $email;
    }
    public function getManagerMobile()
    {
        $manager_id = $this->getManagerId();
        $mobile = $this->where('id',$manager_id)->value('mobile');
        return $mobile;
    }

    public function getManagerCurrency()
    {
        $manager = $this->getLoninManagerInfo();
        return $manager['currency'];
    }
    /**
     * [setManagerSession 设置用户session信息]
     * @param [type] $data        [对象]
     * @param [type] $remember_me [记住我]
     */
    private function setManagerSession($data, $remember_me)
    {
        if ($remember_me == true) {
            cookie('token',$data->id,31536000);
        }
        $login_session_id=session_id();
        session('manager.login_session_id',$login_session_id);
        session('manager.id', $data->id);
        session('manager.name', $data->data['name']);
        session('manager.phone', $data->phone);//halt(session('manager'));
    }
    /**
     * [clearManagerSession 清除用户session信息]
     * @return [type] [description]
     */
    private function clearManagerSession()
    {
        session('manager', null);
    }
    /**
     * [logout 退出登录]
     * @return [type] [description]
     */
    public function logout()
    {
        $this->clearManagerSession();
        cookie('token', null);
        return true;
    }

    /**
     * 创建管理员信息
     * @param  [type] $post_user_info [description]
     * @return [type]                 [description]
     */
    public function createCampManager($post_manager_info)
    {
        return $this->create(array_merge($post_manager_info, ['sign' => \Ramsey\Uuid\Uuid::uuid4()->toString()]));
    }

    public function getCampManagerLists($camp_id)
    {
        return $this->where('camp_id',$camp_id)->select();
    }

    public function getCampManagerInfo($id)
    {
        return $this->where('id',$id)->find();
    }

    public function updateCampManagerInfo($id, $post_campmanager_info)
    {
        return $this->where('id',$id)->update($post_campmanager_info);
    }

    public function delInfo($id)
    {
        $result = $this->destroy($id);
        if ($result) {
            return true;
        }
        return false;
    }

    public function resetPwd($id,$password)
    {
        $campmanagerinfo = $this->where('id',$id)->find();
        $campmanagerinfo->password = $password;
        return $campmanagerinfo->save();
    }
    //把新密码写入session
    public function setPwdSession($phone,$pwd)
    {
        session('admin.phone', $phone);
        session('admin.pwd', $pwd);
    }

    public function revisePwd($phone,$nwepwd)
    {
        $campmanagerinfo = $this->where('phone',$phone)->find();
        $campmanagerinfo->password = $nwepwd;
        return $campmanagerinfo->save();
    }

    private function clearPwdSession()
    {
        session('admin', null);
    }

    //锁定状态
    public function setLuck($id,$status)
    {
        $campmanagerinfo = $this->where('id',$id)->find();
        $campmanagerinfo->status = $status;
        return $campmanagerinfo->save();
    }
    //解除锁定状态
    public function setUnluck($id,$status)
    {
        $campmanagerinfo = $this->where('id',$id)->find();
        $campmanagerinfo->status = $status;
        return $campmanagerinfo->save();
    }
}